package main

import (
	"fmt"
	"io"
	"os"
)

func main() {

	source := "/Users/dean/GoWorks/src/gomod/exercise/xk.png"
	destination := "/Users/dean/GoWorks/src/gomod/exercise/a/xk-copy.png"
	copy(source, destination, 1024)

}

func copy(source, destination string, bufSize int) {
	file, err := os.Open(source)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer file.Close()

	//
	fileDestination, err := os.OpenFile(destination, os.O_RDWR|os.O_CREATE, os.ModePerm)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer fileDestination.Close()

	buf := make([]byte, bufSize)

	for {
		n, err := file.Read(buf)
		if err == io.EOF || n == 0 {
			fmt.Println("文件复制完毕")
			break
		}
		//写入
		n, err = fileDestination.Write(buf[:n]) // 将读到的n前面的数据都写入
		if err != nil {
			fmt.Println("写入失败", err)
		}
	}

}
